package com.eva.statDemo;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import com.eva.statDemo.stat.Debuger;

public class MessageService extends Service {

    Messenger replyMessenger = null;
    Messenger messenger = null;


    @Override
    public IBinder onBind(Intent arg0) {
        messenger = new Messenger(handler);
        return messenger.getBinder();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                replyMessenger = msg.replyTo;
                new MyThread().start();
            }
            super.handleMessage(msg);
        }

    };

    class MyThread extends Thread {
        public void run() {
            Debuger.LOGD("Thread start");
            try {
                Thread.sleep(5 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Debuger.LOGD("Thread finish");
            Message msg = Message.obtain(null, 0);
            try {
                replyMessenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}